package common.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.commons.lang.StringUtils;

import common.constant.GlobalConstant;

/**
 * 日期时间工具类
 * 
 * @author liuxj
 * @version 1.0.0 2014年12月29日
 */
public class DateUtils {

    /**
     * 日期类型转String
     * 
     * @param date
     *            日期类型
     * @param pattern
     *            转换格式
     * @return String
     */
    public static String dateToStr(Date date, String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.format(date);
    }

    /**
     * String转Date类型
     * 
     * @param date
     *            String类型的日期
     * @param pattern
     *            转换格式
     * @return Date类型
     */
    public static Date strToDate(String date, String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        try {
            return sdf.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return null;
    }

    /**
     * 获取时间相差的分钟数
     * 
     * @param beginTime
     *            开始时间
     * @param endTime
     *            结束时间
     * @return 相差的分钟数
     */
    public static long getTimeDifference(String beginTime, String endTime) {
        if (StringUtils.isBlank(beginTime) || StringUtils.isBlank(endTime)) {
            return 0;
        }
        beginTime = beginTime.trim();
        endTime = endTime.trim();

        if (beginTime.length() < 8) {
            beginTime = beginTime + ":00";
        }

        if (endTime.length() < 8) {
            endTime = endTime + ":00";
        }

        Date beginDate = strToDate(beginTime, GlobalConstant.TIME_PATTERN);
        Date endDate = strToDate(endTime, GlobalConstant.TIME_PATTERN);

        long minutes = 0;
        if (beginDate.compareTo(endDate) < 0) {
            long diff = endDate.getTime() - beginDate.getTime();
            minutes = diff / (1000 * 60);
        } else if (beginDate.compareTo(endDate) == 0) {
            minutes = 0;
        } else if (beginDate.compareTo(endDate) > 0) {
            Calendar endCalendar = Calendar.getInstance();
            endCalendar.setTime(endDate);
            endCalendar.add(Calendar.DATE, 1);

            long diff = endCalendar.getTime().getTime() - beginDate.getTime();
            minutes = diff / (1000 * 60);
        }

        return minutes;
    }

    /**
     * 判断当前时间是否在指定的时间区间内
     * 
     * @param beginTime
     *            开始时间
     * @param endTime
     *            结束时间
     * @return true:是；false:否
     */
    public static boolean isAtInterval(String beginTime, String endTime) {
        boolean flag = false;
        if (StringUtils.isBlank(beginTime) || StringUtils.isBlank(endTime)) {
            return flag;
        }
        beginTime = beginTime.trim();
        endTime = endTime.trim();

        if (beginTime.length() < 8) {
            beginTime = beginTime + ":00";
        }

        if (endTime.length() < 8) {
            endTime = endTime + ":00";
        }

        Date today = new Date();
        Date beginDate = strToDate(beginTime, GlobalConstant.TIME_PATTERN);
        Date endDate = strToDate(endTime, GlobalConstant.TIME_PATTERN);

        String todayStr = dateToStr(today, GlobalConstant.DATE_PATTERN);

        Date beginDateTime = strToDate(todayStr + " " + beginTime, GlobalConstant.DATE_TIME_PATTERN);
        Date endDateTime = strToDate(todayStr + " " + endTime, GlobalConstant.DATE_TIME_PATTERN);

        if (beginDate.compareTo(endDate) < 0) {
            if (today.compareTo(beginDateTime) >= 0 && today.compareTo(endDateTime) <= 0) {
                flag = true;
            }
        } else if (beginDate.compareTo(endDate) == 0) {
            flag = true;
        } else if (beginDate.compareTo(endDate) > 0) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(beginDateTime);
            calendar.add(Calendar.DATE, -1);

            if (today.compareTo(calendar.getTime()) >= 0 && today.compareTo(endDateTime) <= 0) {
                flag = true;
            }
        }

        return flag;
    }
//    public static void main(String[] args) {
//        String beginTime = "23:00:00";
//        String endTime = "15:00:00";
//
//        long diff = DateUtils.getTimeDifference(beginTime, endTime);
//        ////System.out.println("分钟：" + diff + ",小时：" + diff / 60);
//
//        ////System.out.println(DateUtils.isAtInterval(beginTime, endTime));
//    }

}
